package ge.telasi.tasks.ui.structure;

import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.LoggableField;
import ge.telasi.tasks.ui.log.Logger;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class StructureSelector extends JPanel implements LoggableField {
    private static final long serialVersionUID = -4588953062339928828L;

    public StructureSelector() {
        initComponents();
        reset();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnChange = new javax.swing.JButton();
        txtStructure = new ge.telasi.tasks.ui.comp.SuggestionText();

        setLayout(new java.awt.BorderLayout(5, 0));

        btnChange.setText("...");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });
        add(btnChange, java.awt.BorderLayout.EAST);

        txtStructure.setEditable(false);
        txtStructure.setSuggestion("აარჩიეთ სტრუქტურული ერთეული");
        add(txtStructure, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        StructureSelectDialog dialog = new StructureSelectDialog(this);
        dialog.setStructure(structure);
        UIUtils.openDialog(dialog);
        if (dialog.isApproved()) {
            structure = dialog.getStructure();
            reset();
        }
}//GEN-LAST:event_btnChangeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChange;
    private ge.telasi.tasks.ui.comp.SuggestionText txtStructure;
    // End of variables declaration//GEN-END:variables

    private Structure structure;

    public Structure getStructure() {
        return structure;
    }

    public void setStructure(Structure structure) {
        this.structure = structure;
        reset();
    }

    private void reset() {
        txtStructure.setText(structure == null ? "" : structure.getName());
        txtStructure.setCaretPosition(0);
    }

    public void setException(Exception ex) {
        txtStructure.setException(ex);
    }

    public Logger getLogger() {
        return txtStructure.getLogger();
    }

    public void setLogger(Logger logger) {
        txtStructure.setLogger(logger);
    }

}
